---
title: "Player Level Tantalus Analysis"
output:
  pdf_document: default
  html_document:
    df_print: paged
  html_notebook: default
  word_document: default
header-includes: \usepackage{dcolumn,rotating}
---

```{r}
setwd("C:/Users/jletchf/git/tantalus-data-analysis/code/Analysis")
options(rstudio.help.showDataPreview = FALSE)
data <- read.csv("playerData.csv")
x = data$threats_made
threats_made_norm = (x-min(x))/(max(x)-min(x))
x = data$revises
revises_norm = (x-min(x))/(max(x)-min(x))
x = data$revises/data$revises_opp

is.nan.data.frame <- function(x)
do.call(cbind, lapply(x, is.nan))
x[is.nan(x)] <- 1
data$frac_revises = x
frac_revises_norm = (x-min(x))/(max(x)-min(x))
Domain = NA
Specificity = NA
Domain_to_deter = NA
```

```{r}
C1_C2 = data[data$condition_domain == 3 & data$condition_domain_threatened == 1,]
C3_C4 = data[data$condition_domain == 1 & data$condition_domain_threatened == 1,]
data2 <- data.frame(revises_norm,Domain,Specificity,Domain_to_deter)
data2$Domain <- NA
data2$threats_made <-  data$threats_made
data2$frac_revises <- data$frac_revises
data2$Age <- data$Age
data2$Gender <- data$Gender
data2$Education <- data$Education
data2$PoliticsRating <- data$PoliticsRating
data2$DeterrencePreference <- data$DeterencePreference
data2$Occupation <- data$Occupation
data2$GeopoliticalConflictKnowledge <- data$GeopoliticalConflictKnowledge
data2$CyberDeterrenceKnowledge <- data$CyberDeterenceKnowledge
data2$StrategyGamesRating <- data$StrategyGamesRating
data2$OnlineStrategyGamesRating <- data$OnlineStrategyGamesRating
data2[data$condition_domain == 3 & data$condition_domain_threatened == 1,]$Domain <- 0
data2[data$condition_domain == 1 & data$condition_domain_threatened == 1,]$Domain <- 1
data3 <- subset(data2,(!is.na(data2$Domain)))
```

```{r}
t.test(C1_C2$threats_made,C3_C4$threats_made)


myLm.1 <- glm(threats_made ~ Domain,family = "poisson",data = data3)

myLm.2 <- lm(frac_revises ~ Domain,data = data3)


myLm.3 <- glm(threats_made ~ Domain + Age + Gender + Education + PoliticsRating + DeterrencePreference + GeopoliticalConflictKnowledge + CyberDeterrenceKnowledge + StrategyGamesRating +OnlineStrategyGamesRating,family = "poisson",data = data3)

myLm.4 <- lm(frac_revises ~ Domain + Age + Gender + Education + PoliticsRating + DeterrencePreference + GeopoliticalConflictKnowledge + CyberDeterrenceKnowledge + StrategyGamesRating +OnlineStrategyGamesRating,data = data3)

```

```{r}
C1_C3 = data[data$condition_specific == 1 & data$condition_domain_threatened == 1,]
C2_C4 = data[data$condition_specific == 0 & data$condition_domain_threatened == 1,]
data2$Specificity <- NA
data2[data$condition_specific == 0 & data$condition_domain_threatened == 1,]$Specificity <- 0
data2[data$condition_specific == 1 & data$condition_domain_threatened == 1,]$Specificity <- 1
data3 <- subset(data2,(!is.na(data2$Specificity)))
```

```{r}


myLm.5 <- glm(threats_made ~ Specificity,family = "poisson",data = data3)

myLm.6 <- lm(frac_revises ~ Specificity,data = data3)


myLm.7 <- glm(threats_made ~ Specificity + Age + Gender + Education + PoliticsRating + DeterrencePreference + GeopoliticalConflictKnowledge + CyberDeterrenceKnowledge + StrategyGamesRating +OnlineStrategyGamesRating,family = "poisson",data = data3)

myLm.8 <- lm(frac_revises ~ Specificity + Age + Gender + Education + PoliticsRating + DeterrencePreference + GeopoliticalConflictKnowledge + CyberDeterrenceKnowledge + StrategyGamesRating +OnlineStrategyGamesRating,data = data3)

```

```{r}
C2_C4 = data[data$condition_specific == 0 & data$condition_domain_threatened == 1,]
C8_C10 = data[data$condition_specific == 0 & data$condition_domain_threatened == 2,]
data2$Domain_to_deter <- NA
data2[data$condition_specific == 0 & data$condition_domain_threatened == 1,]$Domain_to_deter <- 0
data2[data$condition_specific == 0 & data$condition_domain_threatened == 2,]$Domain_to_deter <- 1
data3 <- subset(data2,(!is.na(data2$Domain_to_deter)))
```

```{r}
myLm.9 <- glm(threats_made ~ Domain_to_deter,family = "poisson",data = data3)

myLm.10 <- lm(frac_revises ~ Domain_to_deter,data = data3)


myLm.11 <- glm(threats_made ~ Domain_to_deter + Age + Gender + Education + PoliticsRating + DeterrencePreference + GeopoliticalConflictKnowledge + CyberDeterrenceKnowledge + StrategyGamesRating +OnlineStrategyGamesRating,family = "poisson",data = data3)

myLm.12 <- lm(frac_revises ~ Domain_to_deter + Age + Gender + Education + PoliticsRating + DeterrencePreference + GeopoliticalConflictKnowledge + CyberDeterrenceKnowledge + StrategyGamesRating +OnlineStrategyGamesRating,data = data3)

```

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(stargazer)
```

```{r mylatextable, results = "asis"}
vars.order <- c("Domain","Specificity","Age","Gender","Education","PoliticsRating","DeterrencePreference","GeopoliticalConflictKnowledge","CyberDeterrenceKnowledge","StrategyGamesRating","OnlineStrategyGamesRating")


stargazer(myLm.1,myLm.3,myLm.5,myLm.7,title="Poisson regression models measuring the number of threats as the domain of the threat and the specificity of the threat vary using the player as the unit of analysis. The values in the table body display the regression coefficients with standard errors in parentheses.",align=TRUE,column.labels=c("(1)", "(2)","(3)", "(4)"),model.numbers=FALSE,column.sep.width = "1pt",no.space = TRUE,font.size = "small",order=paste0("^", vars.order , "$"))
#stargazer(myLm.5,myLm.7,title="Poisson regression models measuring the number of threats as the specificity of the threat varies using the player as the unit of analysis. The values in the table body display the regression coefficients with standard errors in parentheses.",align=TRUE,column.labels=c("(3)", "(4)"),model.numbers=FALSE)
stargazer(myLm.2,myLm.4,myLm.6,myLm.8,title="Linear regression models measuring the likelihood of revision as the domain of the threat and the specificity of the threat vary using the player as the unit of analysis. The values in the table body display the regression coefficients with standard errors in parentheses.",align=TRUE,column.labels=c("(5)", "(6)","(7)", "(8)"),model.numbers=FALSE,column.sep.width = "1pt",no.space = TRUE,font.size="small",order=paste0("^", vars.order , "$"))
#stargazer(myLm.6,myLm.8,title="Linear regression models measuring the likelihood of revision as the specificity of the threat varies using the player as the unit of analysis. The values in the table body display the regression coefficients with standard errors in parentheses.",align=TRUE,column.labels=c("(7)", "(8)"),model.numbers=FALSE)
stargazer(myLm.9,myLm.11,myLm.10,myLm.12,title="Poisson and linear regression models measuring number of threats and the likelihood of revision as the domain that the players are attempting to deter varies using the player as the unit of analysis. The values in the table body display the regression coefficients with standard errors in parentheses.",align=TRUE,column.labels=c("(9)", "(10)", "(11)", "(12)"),model.numbers=FALSE)


```

```{r}

data <- read.csv("playerData.csv")

 data$condition = NA

 data[data$condition_domain == 3 & data$condition_domain_threatened == 1 & data$condition_specific == 1,]$condition <- 1
 data[data$condition_domain == 3 & data$condition_domain_threatened == 1 & data$condition_specific == 0,]$condition <- 2
 data[data$condition_domain == 1 & data$condition_domain_threatened == 1 & data$condition_specific == 1,]$condition <- 3
 data[data$condition_domain == 1 & data$condition_domain_threatened == 1 & data$condition_specific == 0,]$condition <- 4
 data[data$condition_domain == 3 & data$condition_specific == 0 & data$condition_domain_threatened == 2,]$condition <- 8
 data[data$condition_domain == 1 & data$condition_specific == 0 & data$condition_domain_threatened == 2,]$condition <- 10

 kruskal.test(Age ~ condition, data = data)

res.aov <- aov(Gender ~ factor(condition), data = data)
summary(res.aov)

kruskal.test(Education ~ condition, data = data)

kruskal.test(PoliticsRating ~ condition, data = data)

kruskal.test(DeterencePreference ~ condition, data = data)

kruskal.test(GeopoliticalConflictKnowledge ~ condition, data = data)

kruskal.test(CyberDeterenceKnowledge ~ condition, data = data)

kruskal.test(StrategyGamesRating ~ condition, data = data)

kruskal.test(OnlineStrategyGamesRating ~ condition, data = data)


```
